package org.daochong.spring.controllor;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.daochong.lang.SimpleHttpClient;
import org.daochong.lang.uai.UniteAccessInterfaceFactory;
import org.daochong.spring.AjaxProxyParams;
import org.daochong.web.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/ap")
public class AjaxProxyControllor extends BaseControllor {

	@Autowired
	private UniteAccessInterfaceFactory uaiFactory;
	
	private Map<String, AjaxProxyParams> caches = new HashMap<String, AjaxProxyParams>();

	private AjaxProxyParams requestParams(String appId) {
		try {
			if (caches.containsKey(appId))
				return caches.get(appId);
			if (this.applicationContext.containsBean(appId)) {
				Object o = this.applicationContext.getBean(appId);
				if (o instanceof AjaxProxyParams) {
					caches.put(appId, (AjaxProxyParams) o);
					return (AjaxProxyParams) o;
				}
			}
			AjaxProxyParams app = this.applicationContext.getBean(AjaxProxyParams.class);
			caches.put(appId, app);
			return app;
		} catch (Throwable e) {
			return null;
		}
	}

	
	@RequestMapping("/{appId}/**")
	public void proxy(@PathVariable String appId, HttpServletRequest request, HttpServletResponse response) {
		String url = request.getRequestURI();
		url = url.substring(url.indexOf("/" + appId + "/") + appId.length() + 1);
		String method = request.getMethod();
		SimpleHttpClient client = uaiFactory.getTemplate(appId, SimpleHttpClient.class);
		if (client == null) {
			throw process(ERROR_AJAX_PROCESS);
		}
		Map<String, String> params = WebUtils.getParameterMap(request);
		AjaxProxyParams app = requestParams(appId);
		if (app != null) {
			params = app.get(appId, params, request, response);
		}
		if (!params.containsKey("noCookie")) {
			Map<String, String> cookies = WebUtils.getCookiesMap(request);
			for (String key : cookies.keySet()) {
				client.addCookies(key, cookies.get(key));
			}
		}
		String re = "";
		if ("GET".equalsIgnoreCase(method)) {
			re = client.getString(url, params);
		} else if ("POST".equalsIgnoreCase(method)) {
			String md = params.get("_method");
			if ("PUT".equalsIgnoreCase(md)) {
				params.remove("_method");
				re = client.putString(url, params);
			} else if ("DELETE".equalsIgnoreCase(md)) {
				params.remove("_method");
				re = client.deleteString(url, params);
			} else {
				re = client.postString(url, params);
			}
		} else if ("PUT".equalsIgnoreCase(method)) {
			re = client.putString(url, params);
		} else if ("DELETE".equalsIgnoreCase(method)) {
			re = client.deleteString(url, params);
		}
		writeJson(re, response);
	}

}
